package com.yueka.controller;

import com.yueka.pojo.R;
import com.yueka.pojo.entity.course.Course;
import com.yueka.pojo.entity.course.CourseQueryParam;
import com.yueka.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/course")
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/list")
    public R list(CourseQueryParam courseQueryParam) {
        try {
            return R.success(courseService.list(courseQueryParam));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/delete")
    public R delete(Integer id) {
        try {
            courseService.delete(id);
            return R.success();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/insert")
    public R insert(@RequestBody Course course) {
        try {
            courseService.insert(course);
            return R.success();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody Course course) {
        try {
            courseService.update(course);
            return R.success();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/byStudentId")
    public R byStudentId(Integer studentId) {
        try {
            return R.success(courseService.byStudentId(studentId));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/getAllTotalLessons")
    public R getAllTotalLessons(Integer studentId) {
        try {
            return R.success(courseService.getallTotalLessons(studentId));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
}
